package me.w70.bot.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

import me.w70.bot.Bot;
import me.w70.bot.account.Account;
import me.w70.bot.gui.util.SortableTableModel;
import me.w70.bot.gui.util.TableSorter;
import me.w70.bot.scripting.Script;

public class ScriptSelector extends JDialog implements ActionListener {
	private boolean DEBUG = false;

	int selectedRow = 0;
	
	JComboBox acc;
	DefaultComboBoxModel accList;

	final JTable table;

	private boolean ALLOW_COLUMN_SELECTION = false;

	private boolean ALLOW_ROW_SELECTION = true;

	private Dimension size = new Dimension(700, 700);

	public ScriptSelector(Window parent) {
		super(parent, "Script Selector", ModalityType.APPLICATION_MODAL);
		Bot.accountHandler.loadAccounts();
		Object[][] data = new Object[Bot.getLoadedScriptsNoDebug().length][3];

		int cs = 0;
		for (Script s : Bot.getLoadedScriptsNoDebug()) {
			if (!s.isUnknown()) {

				try {
					Object[] t = { s.getScriptName(), s.getScriptCategory(),
							s.getScriptAuthor(), s.getScriptDescription() };
					data[cs] = t;
				} catch (Exception e) {
					Object[] t = { s.getScriptName(), s.getScriptCategory(),
							s.getScriptAuthor(), "" };
					data[cs] = t;
				}

				cs++;
			}

		}

		Object[] cols = { "Name", "Category", "Author", "Description" };

		SortableTableModel tm = new SortableTableModel() {
			public Class getColumnClass(int col) {
				switch (col) {
				case 0:
					return String.class;
				case 1:
					return String.class;
				case 2:
					return String.class;
				case 3:
					return String.class;
				default:
					return Object.class;
				}
			}
		};
		tm.setDataVector(data, cols);
		table = new TableSorter(tm);

		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.getTableHeader().setReorderingAllowed(false);

		int namesize = 200;
		int commandsize = 120;
		int authsize = 120;
		int descsize = 300;

		for (int i = 0; i < table.getColumnCount(); i++) {
			TableColumn column = table.getColumnModel().getColumn(i);
			column.setResizable(false);
			switch (i) {
			case 0:
				column.setPreferredWidth(namesize);
				break;
			case 1:
				column.setPreferredWidth(commandsize);
				break;
			case 2:
				column.setPreferredWidth(authsize);
				break;
			case 3:
				column.setPreferredWidth(descsize);
				break;

			default:
				column.setPreferredWidth(200);
				break;
			}

		}

		table.setPreferredScrollableViewportSize(new Dimension(
				size.width - 200, 100));

		table.setFillsViewportHeight(true);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		if (ALLOW_ROW_SELECTION) { // true by default
			ListSelectionModel rowSM = table.getSelectionModel();
			rowSM.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					// Ignore extra messages.
					if (e.getValueIsAdjusting())
						return;

					ListSelectionModel lsm = (ListSelectionModel) e.getSource();
					if (lsm.isSelectionEmpty()) {
						System.out.println("No rows are selected.");
					} else {
						selectedRow = lsm.getMinSelectionIndex();

					}
				}
			});
		} else {
			table.setRowSelectionAllowed(false);
		}

		if (ALLOW_COLUMN_SELECTION) { // false by default
			if (ALLOW_ROW_SELECTION) {
				// We allow both row and column selection, which
				// implies that we *really* want to allow individual
				// cell selection.
				table.setCellSelectionEnabled(true);
			}
			table.setColumnSelectionAllowed(true);
			ListSelectionModel colSM = table.getColumnModel()
					.getSelectionModel();
			colSM.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					// Ignore extra messages.
					if (e.getValueIsAdjusting())
						return;

					ListSelectionModel lsm = (ListSelectionModel) e.getSource();
					if (lsm.isSelectionEmpty()) {
						System.out.println("No columns are selected.");
					} else {
						int selectedCol = lsm.getMinSelectionIndex();
						System.out.println("Column " + selectedCol
								+ " is now selected.");
					}
				}
			});
		}

		// Create the scroll pane and add the table to it.

		JScrollPane scrollPane = new JScrollPane(table);
		JLabel s = new JLabel("  Scripts");
		s.setPreferredSize(new Dimension(size.width, 30));
		add(s, BorderLayout.NORTH);

		// Add the scroll pane to this panel.
		add(scrollPane, BorderLayout.CENTER);
		scrollPane
				.setPreferredSize(new Dimension(size.width, size.height - 200));
		JButton submit = new JButton("Run Script");
		submit.addActionListener(this);
		accList = new DefaultComboBoxModel();
		acc = new JComboBox(accList);
		
		for(Account a : Bot.getAccounts().values()) {
			accList.addElement(a.getUsername());
		}
		JPanel bottom = new JPanel();
		bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
		bottom.add(acc);
		bottom.add(submit);
		add(bottom,  BorderLayout.SOUTH);
		setPreferredSize(new Dimension(size.width, size.height - 200));
		pack();
	}

	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("Run Script")) {
			Bot.setCurrentAccount(Bot.accountHandler.getAccounts().get(acc.getModel().getElementAt(acc.getSelectedIndex())));
			Bot.runScript(table.getModel().getValueAt(selectedRow, 0)
					.toString());
			setVisible(false);
		}

	}

}
